<?php
declare (strict_types=1);

namespace app\admin\model\iot;

use aliyun\AliyunOss;
use app\BaseModel;

/**
 * 取袋路径表模型
 */
class ScanBagStrategy extends BaseModel
{


    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page=1, int $limit=20, string $orderby = ''): array
    {
        $data = self::where($where)
            ->order('key asc')
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }


    /**
     * 通过设备MAC_ID获取取袋路径
     * @param string $mac_id
     * @param string $num
     * @return array
     */
    public static function getMacIdWay(string $mac_id,string $num): array
    {
        //查询取袋路径
        $data = self::alias('a')
            ->field('c.*')
            ->where(['b.mac_id' => $mac_id])
            ->join((new ScanBagStrategyDevice())->getTable() . ' b', 'a.key = b.key')
            ->join((new ScanBagWay())->getTable() . ' c', 'c.id = a.'.$num)
            ->cache(true, 60)
            ->find();
        if ($data) {
            return $data->toArray();
        }
        //判断是v1设备还是v2系统设备
        $where = ['mac_id' => $mac_id];
        $device = Device::where($where)->cache(true, 60)->value('id');
        //默认取袋路径
        $key = $device ? 'V2_Way_1_0' : 'V1_Way_4_0';
        $where = ['key' => $key];
        $data = ScanBagWay::where($where)->cache(true, 60)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * @param string $mac_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function checkMacIdWay(string $mac_id): array
    {
        //查询取袋路径
        $data = self::alias('a')
            ->field('b.*')
            ->where(['b.mac_id' => $mac_id])
            ->join((new ScanBagStrategyDevice())->getTable() . ' b', 'a.key = b.key')
            ->cache(true, 60)
            ->find();

        return $data ? $data->toArray() : [];
    }




}




